package com.shanghai.shanghaielectric.UI;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.shanghai.shanghaielectric.Constants;
import com.shanghai.shanghaielectric.Model.EventBus.EventbusModel_Tag;
import com.shanghai.shanghaielectric.MainActivity;
import com.shanghai.shanghaielectric.R;
import com.shanghai.shanghaielectric.UI.DilaogView.Dialog_Alert;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import shanghaielectric_lib.Communication.Comm_Modbus.ModbusThread;
import shanghaielectric_lib.MyEventBus.EventBus_int_bool;
import shanghaielectric_lib.MyEventBus.MyEventBusModel;
import shanghaielectric_lib.Utils.LogUtil;


public class ScanFragment extends Fragment {
    public List<Fragment> eventfragments = new ArrayList<Fragment>();
    public View scanView;
    List<ImageView> imageViewList;
    @BindView(R.id.iv_scan_unit_1)
    ImageView ivScanUnit1;
    @BindView(R.id.iv_scan_unit_2)
    ImageView ivScanUnit2;
    @BindView(R.id.iv_scan_unit_3)
    ImageView ivScanUnit3;
    @BindView(R.id.iv_scan_unit_4)
    ImageView ivScanUnit4;
    @BindView(R.id.iv_scan_unit_5)
    ImageView ivScanUnit5;
    @BindView(R.id.iv_scan_unit_6)
    ImageView ivScanUnit6;
    @BindView(R.id.iv_scan_unit_7)
    ImageView ivScanUnit7;
    @BindView(R.id.iv_scan_unit_8)
    ImageView ivScanUnit8;
    @BindView(R.id.bt_main_setting)
    Button btMainSetting;
    @BindView(R.id.bt_main_Home)
    Button btMainHome;
    @BindView(R.id.bt_main_scan)
    Button btMainScan;
    Unbinder unbinder;
    boolean idScanRunning = false;

    private MainActivity mainActivity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        System.out.println("Event____onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("Event____onCreate");
        mainActivity = (MainActivity) getActivity();
        EventBus.getDefault().register(this);
    }

    private void initView() {
        imageViewList = new ArrayList<>();
        imageViewList.add(ivScanUnit1);
        imageViewList.add(ivScanUnit2);
        imageViewList.add(ivScanUnit3);
        imageViewList.add(ivScanUnit4);
        imageViewList.add(ivScanUnit5);
        imageViewList.add(ivScanUnit6);
        imageViewList.add(ivScanUnit7);
        imageViewList.add(ivScanUnit8);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        System.out.println("Event____onCreateView");
        scanView = inflater.inflate(R.layout.fragment_scan, container,
                false);
        unbinder = ButterKnife.bind(this, scanView);
        initView();
        return scanView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        System.out.println("Event____onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        System.out.println("Event____onStart");
    }

    @Override
    public void onResume() {
        super.onResume();

        System.out.println("Event____onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        System.out.println("Event____onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        System.out.println("Event____onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        System.out.println("Event____onDestroyView");
        unbinder.unbind();
//反注册
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("Event____onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        System.out.println("Event____onDetach");
    }


    @OnClick({R.id.iv_scan_unit_1, R.id.iv_scan_unit_2, R.id.iv_scan_unit_3, R.id.iv_scan_unit_4, R.id.iv_scan_unit_5, R.id.iv_scan_unit_6, R.id.iv_scan_unit_7, R.id.iv_scan_unit_8, R.id.bt_main_setting, R.id.bt_main_Home, R.id.bt_main_scan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_main_Home:
                showDiolg("是否开启HOME页面？");
                Dialog_Alert.setDiolgAlertListener(new Dialog_Alert.DialogAlertListener() {
                    @Override
                    public void onClink(boolean flag) {
                        if (flag) {
                            mainActivity.setTable(1);
                        }
                    }
                });
                break;
            case R.id.bt_main_scan:
//                if (idScanRunning){
//                    Toast.makeText(mainActivity, "正在扫描请等待...", Toast.LENGTH_SHORT).show();
//                }else {
//                    setScanLight(5, 300);
//
//                }
                showDiolg("是否扫描设备？");
                Dialog_Alert.setDiolgAlertListener(new Dialog_Alert.DialogAlertListener() {
                    @Override
                    public void onClink(boolean flag) {
                        if (flag) {
                            ModbusThread.refreshCommunicate();
                        }
                    }
                });
                break;
            case R.id.bt_main_setting:
                showDiolg("是否进入设置界面？");
                Dialog_Alert.setDiolgAlertListener(new Dialog_Alert.DialogAlertListener() {
                    @Override
                    public void onClink(boolean flag) {
                        if (flag) {
                            mainActivity.setTable(3);
                        }
                    }
                });
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void changeUI(MyEventBusModel myEventBusModel) {
        if (myEventBusModel.dataType != null && myEventBusModel.eventDescripe != null)
            if (myEventBusModel.dataType.equals("long") && myEventBusModel.eventDescripe.equals("scan")) {
                LogUtil.e("", "收到通知");
                if (myEventBusModel.flag) {
                    for (int i = 0; i < imageViewList.size(); i++) {
                        if (myEventBusModel.id == i) {
                            imageViewList.get(i).setImageResource(R.drawable.point_green);
                        } else {
                            imageViewList.get(i).setImageResource(R.drawable.point_red);
                        }
                    }
                } else {
                    imageViewList.get(imageViewList.size() - 1).setImageResource(R.drawable.point_red);
                }
            }
    }

    public void setUnitLightState(int index, boolean flag) {
        int resourceID = flag ? R.drawable.point_green : R.drawable.point_red;
        imageViewList.get(index).setImageResource(resourceID);
    }

    public void setScanLight(final int count, final long time) {
        final MyEventBusModel myEventBusModel = new MyEventBusModel();
        new Thread(new Runnable() {
            @Override
            public void run() {
                idScanRunning = true;
                for (int i = 0; i < count; i++) {
                    for (int j = 0; j < imageViewList.size(); j++) {
                        myEventBusModel.dataType = "long";
                        myEventBusModel.eventDescripe = "scan";
                        myEventBusModel.id = j;
                        myEventBusModel.flag = true;
                        try {
                            Thread.sleep(time);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        EventBus.getDefault().post(myEventBusModel);
                    }
                }
                myEventBusModel.flag = false;
                EventBus.getDefault().post(myEventBusModel);
                idScanRunning = false;
            }
        }).start();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveDeviceState(EventBus_int_bool eventBusIntBool) {
        int resourceID = eventBusIntBool.flag ? R.drawable.point_green : R.drawable.point_red;
        imageViewList.get(eventBusIntBool.id - 1).setImageResource(resourceID);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateConnectedState(EventbusModel_Tag eventbusModel_tag) {
        if (EventbusModel_Tag.UPDATE_COMMUNICATE.equals(eventbusModel_tag.tag)) {
            for (int i = 0; i < imageViewList.size(); i++) {
             if (Constants.deviceStates[i].isConnect){
                 imageViewList.get(i).setImageResource(R.drawable.point_green);
             }
            }
        }
    }

    private void showDiolg(String commandName) {
        Dialog_Alert diolgAlert = new Dialog_Alert();
        diolgAlert.setCommandName(commandName);
        diolgAlert.show(this.getFragmentManager(), "");
    }
}
